var app = getApp();
var com = require('../../common.js');
Page({
  data: {
    login: 'pass',
    statusd: 1,
    phoneNum: '获取验证码',
    bindEndClick: 'clickEnd',
    number: 61,
    imageCode: '',
    imageImg: '',
    phoneNumber: '',
    code: '',
    phoneKey: '',
    phoneInput: '',
    inpShow: false,
    inputShow: false,
    inputType: 'password'
  },
  onLoad: function(options) {
    var _this = this
    if (wx.getStorageSync('token')) {
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
    wx.hideTabBar({})
    setTimeout(() => {
      _this.setData({
        statusd: 0
      })
    }, 200)
  },
  //切换密码状态
  onShow() {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2
      })
    }
  },
  eyeTap() {
    var _this = this,
      type = _this.data.inputType
    _this.setData({
      inputType: type == 'password' ? '' : 'password'
    })
  },
  //切换登陆状态
  login(e) {
    var id = e.currentTarget.id
    var _this = this
    if (id == 'pass') {
      _this.setData({
        number: 0
      })
    } else {
      _this.setData({
        phoneNum: '获取验证码',
        bindEndClick: 'clickEnd',
        number: 61
      })
      _this.onObtain()
    }
    this.setData({
      login: id,
      statusd: 1
    })
    setTimeout(() => {
      _this.setData({
        statusd: 0
      })
    }, 200)
  },
  //倒计时
  onSetTimeout() {
    var _this = this
    var numberNum = parseInt(_this.data.number)
    numberNum = numberNum - 1
    if (_this.data.number == 0) {
      _this.setData({
        phoneNum: "重新获取",
        bindEndClick: 'clickEnd',
        number: 61
      })
      return false
    } else {
      _this.setData({
        phoneNum: numberNum + '秒后重新获取',
        bindEndClick: '',
        number: numberNum
      })
    }
    setTimeout(() => {
      _this.onSetTimeout()
    }, 1000)
  },
  //获取输入的手机号
  phoneNumber(e) {
    var _this = this,
      id = e.currentTarget.id
    if (id == 1) {
      _this.setData({
        phoneNumber: e.detail.value
      })
      if (_this.data.phoneNumber != '') {
        _this.setData({
          inputShow: true
        })
      } else {
        _this.setData({
          inputShow: false
        })
      }
    } else if (id == 2) {
      _this.setData({
        code: e.detail.value
      })
    } else {
      _this.setData({
        phoneInput: e.detail.value
      })
      if (_this.data.phoneInput != '') {
        _this.setData({
          inpShow: true
        })
      } else {
        _this.setData({
          inpShow: false
        })
      }
    }
  },
  //点击倒计时
  clickEnd() {
    var _this = this,
      data = {
        captcha: _this.data.code,
        phone_number: _this.data.phoneNumber,
        key: _this.data.imageCode,
      },
      url = app.link.basics
    com.link(url, data, function(rep) {
      console.log(rep.data.response.captcha)
      if (rep.data.state == false) {
        // _this.onObtain()
        com.toast(rep.data.message)
      } else {
        com.toast(rep.data.message)
        _this.setData({
          phoneKey: rep.data.response.key
        })
        _this.onSetTimeout()
      }
    })
  },
  //提交
  formSubmit(e) {
    console.log(e);
    wx.showToast({
      title: '登录中',
      icon: 'loading'
    })
    var _this = this,
      url = app.link.login,
      data = e.detail.value,
      type = _this.data.login,
      urls = app.link.basicsLogin,
      datas = {
        captcha: e.detail.value.captcha,
        phone_number: _this.data.phoneNumber,
        key: _this.data.phoneKey,
      }
    if (type == 'pass') {
      com.link(url, data, function(rep) {
        console.log('-=-=--=', rep)
        if (!rep.data.state) {
          com.toast(rep.data.message)
          return false;
        }
        wx.setStorageSync('phone', rep.data.response.user.phone_number)
        com.toast(rep.data.state == true ? '登陆成功' : rep.data.message);
        if (rep.data.state == true) {
          wx.setStorageSync('token', rep.data.response.token)
          console.log(wx.getStorageSync('token'))
          let url = app.link.appraisal,
            data = {}
          com.link(url, data, function(rep) {
            console.log(rep)
            wx.setStorageSync('store', rep.data.response.response.store)
            if (rep.data.response.response.store <= 0) {
              wx.redirectTo({
                url: '/pages/examine/examine?id=1'
              })
            } else if (rep.data.response.response.store > 0 && rep.data.response.response.examine == false && rep.data.response.response.contract != false) {
              wx.redirectTo({
                url: '/pages/examine/examine?id=2'
              })
            } else if (rep.data.response.response.contract == false && rep.data.response.response.store > 0) {
              wx.redirectTo({
                url: '/pages/examine/examine?id=3'
              })
            } else {
              setTimeout(() => {
                wx.switchTab({
                  url: '/pages/index/index'
                })
              }, 1500)
            }
            wx.hideToast()
          }, 'GET')
        }
      })
    } else {
      com.link(urls, datas, function(rep) {
        console.log(rep)
        if (!rep.data.state) {
          com.toast(rep.data.message)
          return false;
        }
        wx.setStorageSync('phone', rep.data.response.user.phone_number)
        wx.setStorageSync('token', rep.data.response.token)
        com.toast(rep.data.state == true ? '登陆成功' : rep.data.message);
        if (rep.data.state == true && rep.data.response.user.is_boss != false) {
          let url = app.link.appraisal,
            data = {}
          com.link(url, data, function(rep) {
            console.log(rep)
            wx.setStorageSync('store', rep.data.response.response.store)
            if (rep.data.response.response.store <= 0) {
              wx.redirectTo({
                url: '/pages/examine/examine?id=1'
              })
            } else if (rep.data.response.response.store > 0 && rep.data.response.response.examine == false) {
              wx.redirectTo({
                url: '/pages/examine/examine?id=2'
              })
            } else {
              setTimeout(() => {
                wx.switchTab({
                  url: '/pages/index/index'
                })
              }, 1500)
            }

          }, 'GET')
        } else {
          setTimeout(() => {
            wx.switchTab({
              url: '/pages/index/index'
            })
          }, 1500)
        }
        wx.hideToast()
      })
    }

  },
  //获取验证码
  onObtain() {
    var _this = this,
      url = app.link.obtain,
      data = {}
    com.link(url, data, function(rep) {
      console.log(rep)
      wx.hideToast()
      _this.setData({
        imageCode: rep.data.response.captcha.key,
        imageImg: rep.data.response.captcha.img,
      })
    }, 'GET')
  },
  //刷新验证码
  f5codeImg() {
    var _ = this
    _.onObtain()
  },
  //清空输入框
  clear(e) {
    var _this = this,
      id = e.currentTarget.id
    if (id == 1) {
      _this.setData({
        phoneInput: '',
        inpShow: false
      })
    } else {
      _this.setData({
        phoneNumber: '',
        inputShow: false
      })
    }

  }
})